package com.sy.structural.flyweight;

import java.util.HashMap;
import java.util.Map;

public class ZuDao {

    private static Map<String, AbstractWaiterFlyWeight> pool = new HashMap<>();

    static {
        Waiters waiter1 = new Waiters("shen");
        Waiters waiter2 = new Waiters("yong");
        pool.put(waiter1.getName(), waiter1);
        pool.put(waiter2.getName(), waiter2);
    }

    public static AbstractWaiterFlyWeight getFlyWeight(String name) {
        AbstractWaiterFlyWeight abstractWaiterFlyWeight = pool.get(name);
        if (abstractWaiterFlyWeight == null) {
            for (AbstractWaiterFlyWeight value: pool.values()) {
                if (value.isCanService()) {
                    return value;
                }
            }
            return null;
        }

        return abstractWaiterFlyWeight;
    }

}
